using System;

namespace RapidHDL
{
	/// <summary>
	/// Summary description for TextFile.
	/// </summary>
	public class TextFile
	{
		public TextFile()
		{
		}

		public static bool FolderExists(string psDirectory)
		{
			if (!System.IO.Directory.Exists(psDirectory))
				return false;
			return true;
		}


		public static bool FileExists(string psFileName)
		{
			return System.IO.File.Exists(psFileName);
		}

		public static bool FileExists(string psDirectory, string psFileName)
		{
			if (!System.IO.Directory.Exists(psDirectory))
				return false;
			
			return FileExists(BuildFileName(psDirectory,psFileName));
		}

        public static bool CheckFilesMatch(string psDirectory, string psFileName1, string psFileName2, int piStartLine)
        {
            return CheckFilesMatch(psDirectory, psFileName1, psDirectory, psFileName2, piStartLine);
        }

		public static bool CheckFilesMatch(string psDirectory1, string psFileName1, string psDirectory2, string psFileName2, int piStartLine)
		{
			int iLineCount = 1;
			System.IO.TextReader oTextReader1;
			System.IO.TextReader oTextReader2;

			string sLine1;
			string sLine2;

			if (!FileExists(psDirectory1,psFileName1) | !FileExists(psDirectory2,psFileName2))
				return false;

			oTextReader1 = OpenFileRead(psDirectory1,psFileName1);
			oTextReader2 = OpenFileRead(psDirectory2,psFileName2);
            
			sLine1 = oTextReader1.ReadLine();
			sLine2 = oTextReader2.ReadLine();
			while (iLineCount < piStartLine & sLine1 != null & sLine2 != null)
			{
				iLineCount++;
				sLine1 = oTextReader1.ReadLine();
				sLine2 = oTextReader2.ReadLine();
			}

			while(sLine1 == sLine2 & sLine1 != null & sLine2 != null)
			{
				iLineCount++;
				sLine1 = oTextReader1.ReadLine();
				sLine2 = oTextReader2.ReadLine();
			}

			oTextReader1.Close();
			oTextReader2.Close();

			if(sLine1 != sLine2)
				return false;

			return true;
		}

		public static void DeleteFile(string psDirectory, string psFileName)
		{
			if (FileExists(psDirectory,psFileName))
				System.IO.File.Delete(BuildFileName(psDirectory,psFileName));
		}

		public static string BuildFileName(string psDirectory, string psFileName)
		{
			string sFileName = psDirectory;

            if (!Utility.IsUnix())
            {
                if (!sFileName.EndsWith("\\"))
                    sFileName += "\\";
            }
            else
            {
                if (sFileName == "")
                    return psFileName;

                sFileName = sFileName.Replace("\\", "/");

                if (!sFileName.EndsWith("/"))
                    sFileName += "/";
            }			
			
			sFileName += psFileName;
			return sFileName;
		}

		public static System.IO.StreamReader OpenFileRead(string psFileName)
		{
            if (!FileExists(psFileName))
                throw new Exception("Read file " + psFileName.ToString() + " does not exist.");

			return System.IO.File.OpenText(psFileName);
		}

		public static System.IO.StreamReader OpenFileRead(string psDirectory, string psFileName)
		{
			return OpenFileRead(BuildFileName(psDirectory,psFileName));
		}


		public static System.IO.StreamWriter OpenFileReWrite(string psFileName)
		{
			return System.IO.File.CreateText(psFileName);
		}

		public static System.IO.StreamWriter OpenFileReWrite(string psDirectory, string psFileName)
		{
			if (!FolderExists(psDirectory))
				throw new Exception("Directory " + psDirectory.ToString() + " does not exist.");

			return OpenFileReWrite(BuildFileName(psDirectory,psFileName));
		}

		public static void WriteFileText(System.IO.StreamWriter psFileStream, string psText)
		{
			psFileStream.WriteLine(psText);
		}

		public static void WipeDirectory(string psDirectory,string psSearchPattern)
		{
			if(!FolderExists(psDirectory))
				return;

			System.IO.DirectoryInfo oDirectoryInfo = new System.IO.DirectoryInfo(psDirectory);

			foreach(System.IO.FileInfo oFileInfo in oDirectoryInfo.GetFiles(psSearchPattern ))
			{
				oFileInfo.Delete();
			}
		}

        public static string GetExtention(string psFileName)
        {
            int iPos = psFileName.LastIndexOf('.');
            if (iPos == -1) return "";
            return psFileName.Substring(iPos + 1, psFileName.Length - iPos - 1);           
        }

        public static string ReadEntireFile(string psFileName)
        {
            System.IO.StreamReader oFile = OpenFileRead(psFileName);
            string sResult = oFile.ReadToEnd();
            oFile.Close();
            return sResult;
        }

        public static void ConvertToUnix(string psFileName)
        {
            string sFile = ReadEntireFile(psFileName);
            sFile = sFile.Replace("\r","");
            System.IO.StreamWriter oFile = OpenFileReWrite(psFileName);
            oFile.Write(sFile);
            oFile.Close();
        }

	}
}
